package com.hjq.base.util

import java.math.BigDecimal

class TextFormatUtil {
    companion object{
        //保留两位小数
        fun formatDoubleTwo(price: Double?):String{
            var price = price
            if (price == null) {
                price = java.lang.Double.valueOf(0.0)
            }
            var number = BigDecimal(price.toString())
            number = number.setScale(2, 4)
            return number.toPlainString()
        }
        //保留整数
        fun formatInt(price: Double?):String{
            var price = price
            if (price == null) {
                price = java.lang.Double.valueOf(0.0)
            }
            var number = BigDecimal(price.toString())
            number = number.setScale(0, 4)
            return number.toPlainString()
        }

        fun getBD(price: Double?): BigDecimal {
            var price = price
            if (price == null) {
                price = java.lang.Double.valueOf(0.0)
            }
            return BigDecimal(price.toString())
        }

        fun getBDByStr(price: String?): BigDecimal {
            return BigDecimal(price)
        }

        fun getPriceStr(bigDecimal: BigDecimal): String {
            var bigDecimal = bigDecimal
            val zero = BigDecimal.ZERO
            return if (bigDecimal.compareTo(zero) == 0) {
                bigDecimal = zero
                bigDecimal.toPlainString()
            } else {
                bigDecimal.stripTrailingZeros().toPlainString()
            }
        }
    }
}